package com.wawei.seeds.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wawei.seeds.R;
import com.wawei.seeds.model.CommentModel;
import com.wawei.seeds.utils.ImageLoader;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

/**
 * Created by itachi on 15/8/1.
 */
public class ChampionCommentAdapter extends BaseAdapter {
    private List<CommentModel> info;
    private LayoutInflater mLayoutInflater;
    ImageLoader imageLoader;

    public ChampionCommentAdapter(List<CommentModel> info, Context context) {
        mLayoutInflater = LayoutInflater.from(context);
        this.imageLoader = new ImageLoader(context);
        this.info = info;
    }

    @Override
    public int getCount() {
        return info == null ? 0 : info.size();
    }

    @Override
    public CommentModel getItem(int position) {
        return info.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CommentItemViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new CommentItemViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.champion_list_item,
                    parent, false);
            viewHolder.nameView = (TextView)convertView.findViewById(R.id.comment_person);
            viewHolder.commentView = (TextView)convertView.findViewById(R.id.comment_content);
            viewHolder.commentTime = (TextView)convertView.findViewById(R.id.comment_time);
            viewHolder.logoView = (ImageView)convertView.findViewById(R.id.item_appointment_hero_logo);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (CommentItemViewHolder) convertView.getTag();
        }

        CommentModel item = getItem(position);
        if (viewHolder != null && viewHolder.nameView != null) {
            viewHolder.nameView.setText(item.getUserNick());
        }
        if (viewHolder != null && viewHolder.commentView != null) {
            viewHolder.commentView.setText(item.getContent());
        }
        if (viewHolder != null && viewHolder.commentTime != null) {
            viewHolder.commentTime.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(item.getCreateTime() * 1000));
        }

        if (viewHolder != null && viewHolder.logoView != null) {
            if (item.getUserIcon() == null || item.getUserIcon().length() == 0) {
                imageLoader.loadImageByResources(R.drawable.walnut_logo, viewHolder.logoView, true, false);
            } else {
                imageLoader.loadImage(item.getUserIcon(), viewHolder.logoView, true, false);
            }
        }

        return convertView;
    }

    public void refreshData(CommentModel model) {
        if (info != null) {
            info.add(0, model);

            notifyDataSetChanged();
        }
    }

    private static class CommentItemViewHolder {
        TextView nameView;
        TextView commentView;
        TextView commentTime;
        ImageView logoView;
    }
}
